package com.voute.Presenter.Impl;

import android.content.Context;

import com.voute.Model.Bean.BaseBean;
import com.voute.Model.Bean.SimpleNotificationBean;
import com.voute.Model.Bean.UnreadNotificationBean;
import com.voute.Model.IPushNotificationModel;
import com.voute.Model.Impl.PushNotificationMoImpl;
import com.voute.Presenter.IPushNotificationPresenter;
import com.voute.Utils.CommonSubscriber;
import com.voute.Utils.TransformUtils;
import com.voute.View.IView.IPushMessageView;

/**
 * Created by xianke on 22/04/2017.
 */

public class PushNotificationPresenterImpl implements IPushNotificationPresenter {

    private IPushMessageView push_view;
    private Context context;
    private IPushNotificationModel model;

    public PushNotificationPresenterImpl(IPushMessageView view, Context c) {
        push_view = view;
        context = c;
        model = new PushNotificationMoImpl();
    }

    @Override
    public void requestPushNotifications(String page) {
        model.requestPushNotifications(page).compose(TransformUtils.<SimpleNotificationBean>defaultSchedulers()).subscribe(new CommonSubscriber<SimpleNotificationBean>() {
            @Override
            public void onSuccess(SimpleNotificationBean response) {
                push_view.hideLoading();
                push_view.refreshMessages(response);
            }

            @Override
            public void onFailure(BaseBean response) {
                push_view.hideLoading();
                push_view.showError(response.getMsg());
            }
        });
    }

    @Override
    public void requestUnreadNotificationNum() {
        model.requestUnreadNotificationNum().compose(TransformUtils.<UnreadNotificationBean>defaultSchedulers()).subscribe(new CommonSubscriber<UnreadNotificationBean>() {
            @Override
            public void onSuccess(UnreadNotificationBean response) {
                push_view.setUnreadMessageNum(response.getData().getCount());
            }

            @Override
            public void onFailure(BaseBean response) {
                push_view.showError(response.getMsg());
            }
        });
    }

    @Override
    public void setAllNessageReaded() {
        model.setAllNessageReaded().compose(TransformUtils.<BaseBean>defaultSchedulers()).subscribe(new CommonSubscriber<BaseBean>() {
            @Override
            public void onSuccess(BaseBean response) {
                push_view.setAllReadedSuccess();
            }

            @Override
            public void onFailure(BaseBean response) {
                push_view.showError(response.getMsg());
            }
        });
    }
}
